package com.ggh.jinjilive.ui.mall.goods.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemMallGoodsPointsBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.MallPointsClass;

import java.util.List;

public class GoodsPointsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<MallPointsClass> mallPointsClassList;
    private int TheCurrent = 0;

    public void setAdd(List<MallPointsClass> mallPointsClassList) {
        this.mallPointsClassList = mallPointsClassList;
        notifyDataSetChanged();
    }


    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMallGoodsPointsBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_mall_goods_points, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
        if(recyclerViewholder instanceof ItemViewHolder){
            ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
            MallPointsClass mallPointsClass = mallPointsClassList.get(position);
            if (null != mallPointsClass) {
                if(mallPointsClass.getSelected()){
                    holder.binding.tvClassification.setTextColor(Color.parseColor("#347EFF"));
                    holder.binding.tvClassification.setBackgroundColor(Color.parseColor("#80ffffff"));
                }else {
                    holder.binding.tvClassification.setTextColor(Color.parseColor("#FFFFFF"));
                    holder.binding.tvClassification.setBackgroundColor(0);
                }
                holder.binding.tvClassification.setText(mallPointsClass.getName());
                holder.binding.tvClassification.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!mallPointsClass.getSelected()){
                            mallPointsClassList.get(TheCurrent).setSelected(false);
                            notifyItemChanged(TheCurrent);
                            mallPointsClassList.get(position).setSelected(true);
                            TheCurrent = position;
                            notifyItemChanged(position);
                            if(null != addItemOnClickListener){
                                addItemOnClickListener.onSkipClicked(position,mallPointsClass);
                            }
                        }
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return null == mallPointsClassList ? 0 : mallPointsClassList.size();
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemMallGoodsPointsBinding binding;
        public ItemViewHolder(@NonNull ItemMallGoodsPointsBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}